package chunk.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JFrame;

import wucoke.tools.HeroDirection;

import chunk.control.GameManager;
import chunk.model.Box;
import chunk.model.Hero;

public class MainFrame extends JFrame {


	private static final long serialVersionUID = 1L;
	private GameManager gameMamager = null;
	private int width  = 500;
	private int height = 800;


	private MyPanel myPanel = null;


	public MainFrame(GameManager gameManager) {

		this.gameMamager = gameManager;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		setSize(width, height);
		setLocation((screenSize.width - width) / 2,
				(screenSize.height - height) / 2);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		
		setResizable(false);

		myPanel = new MyPanel(this.gameMamager);
		myPanel.setLayout(null);
		myPanel.setSize(this.width, this.height);
		
		
		

		getContentPane().add(myPanel);
		myPanel.addKeyListener(new KeyAdapter() {
			
			Hero hero = myPanel.getHero();
		
	
			@Override
			public void keyPressed(KeyEvent keyEvent) {

				// System.out.println("key pressed" +i++);
				if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {

					hero.setDown(true);

				} else if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {
					hero.setUP(true);

				} else if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
					hero.setLeft(true);

				} else if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
					hero.setRight(true);

				}

				hero.changDir();
				hero.move();
			}
			
			@Override
			public void keyReleased(KeyEvent keyEvent) {
				
				if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {

					hero.setDown(false);

				} else if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {
					hero.setUP(false);

				} else if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
					hero.setLeft(false);

				} else if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
					hero.setRight(false);

				}
				
				hero.changDir();

			}
		});
		
		

	}


	public void addBoxs() {

		int temp1 = 30;

		for (int i = 0; i < 1; i++) {

			Box box = new Box(temp1, 0, 10, 10, 5, Color.blue, this.gameMamager);
			box.decideDirect();
			myPanel.addBox(box);

			temp1 += 100;
		}

		int temp2 = 30;
		for (int i = 0; i < 1; i++) {

			Box box = new Box(0, temp2, 10, 10, 5, Color.blue, this.gameMamager);
			box.decideDirect();
			myPanel.addBox(box);

			temp2 += 100;
		}

		int temp3 = 30;

		for (int i = 0; i < 1; i++) {

			Box box = new Box(this.width - 20, temp3, 10, 10, 5, Color.blue,
					this.gameMamager);
			box.decideDirect();
			myPanel.addBox(box);

			temp3 += 100;
		}
		
		int temp4 = 30;

		for (int i = 0; i < 1; i++) {

			Box box = new Box(temp4, this.height - 20, 10, 10, 5, Color.blue,
					this.gameMamager);
			box.decideDirect();
			myPanel.addBox(box);

			temp4 += 100;
		}

	}


	public int getHeight() {
		return height;
	}
	public MyPanel getMyPanel() {
		return myPanel;
	}
	


	public int getWidth() {
		return width;
	}
	
	
	public void setHeight(int height) {
		this.height = height;
	}

	public void setMyPanel(MyPanel myPanel) {
		this.myPanel = myPanel;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	
	


	
	



}
